package api

import (
	"a_plus_station/dto_out"
	"a_plus_station/global"
	"a_plus_station/tables"
	"github.com/gin-gonic/gin"
	"net/http"
)

// 获取分类的列表
func CategoryList(ctx *gin.Context) {
	var category tables.Category
	categorysList, count, err := category.CategoryList(ctx, global.DB)
	if err != nil {
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg": "查询失败",
		})
		return
	}

	//var categoryLi []dto_out.CategoryInfo
	categoryLiMain := make([]dto_out.CategoryInfo, 0, 9)
	categoryLiFirst := make([]dto_out.CategoryInfo, 0, 5)
	categoryLiSecond := make([]dto_out.CategoryInfo, 0, 5)
	categoryLiThird := make([]dto_out.CategoryInfo, 0, 5)
	categoryLiOther := make([]dto_out.CategoryInfo, 0, 5)

	for _, cate := range *categorysList {
		if in(cate.Name, []string{"Apparel & Outerwear", "Baby, Toddler, & Child Items", "Bags & Luggage", "Caps & Hats", "Drinkware", "Toys, Games, & Novelties", "Pet Supplies", "School, Office, & Craft Supplies", "Sports, Fitness, & Outdoors", "Product Customization"}) {
			categoryLiMain = append(categoryLiMain, dto_out.CategoryInfo{
				ID:    cate.ID,
				Name:  cate.Name,
				Image: cate.ImageUrl,
			})
		} else if in(cate.Name, []string{"Writing Instruments", "Home & Garden", "Key Chains", "Kitchen & Dining", "Party Supplies"}) {
			categoryLiFirst = append(categoryLiFirst, dto_out.CategoryInfo{
				ID:    cate.ID,
				Name:  cate.Name,
				Image: cate.ImageUrl,
			})
		} else if in(cate.Name, []string{"Health, Beauty & Wellness", "Holiday & Religious Items", "Jewelry and Accessories", "Lanyards, Badges, & Badge Holders", "Signs, Banners, Flags, & Displays"}) {
			categoryLiSecond = append(categoryLiSecond, dto_out.CategoryInfo{
				ID:    cate.ID,
				Name:  cate.Name,
				Image: cate.ImageUrl,
			})
		} else if in(cate.Name, []string{"Automotive Accessories", "Awards & Trophies", "Publications", "Storage & Organization", "Other"}) {
			categoryLiThird = append(categoryLiThird, dto_out.CategoryInfo{
				ID:    cate.ID,
				Name:  cate.Name,
				Image: cate.ImageUrl,
			})
		} else {
			categoryLiOther = append(categoryLiOther, dto_out.CategoryInfo{
				ID:    cate.ID,
				Name:  cate.Name,
				Image: cate.ImageUrl,
			})
		}

	}
	categoryOutInfoType := dto_out.CategoryInfoType{
		Main:   categoryLiMain,
		First:  categoryLiFirst,
		Second: categoryLiSecond,
		Third:  categoryLiThird,
		Other:  categoryLiOther,
	}
	cateOut := dto_out.CategoryOut{
		CategoryList: categoryOutInfoType,
		Count:        *count,
	}
	// 返回
	//{
	//      "main": [{id: 1, name: "Apparel & Outerwear"}, {id: 2, name: "Baby, Toddler, & Child Items"}, {id: 3, name: "Bags & Luggage"}, {id: 4, name: "Caps & Hats"},{id: 5, name: "Drinkware"}, {id: 6, name: "Toys, Games, & Novelties"}, {id: 7, name: "Pet Supplies"}, {id: 8, name: "School, Office, & Craft Supplies"}, {id: 9, name: "Sports, Fitness, & Outdoors"}],
	//      "first": [{id: 10, name: "Writing Instruments"}, {id: 11, name: "Home & Garden"}, {id: 12, name: "Key Chains"}, {id: 13, name: "Kitchen & Dining"}, {id: 14, name: "Party Supplies"}],
	//      "second": [{id: 15, name: "Health, Beauty & Wellness"}, {id: 16, name: "Holiday & Religious Items"}, {id: 17, name: "Jewelry and Accessories"}, {id: 18, name: "Lanyards, Badges, & Badge Holders"}, {id: 19, name: "Signs, Banners, Flags, & Displays"}],
	//      "third": [{id: 20, name: "Tools & Hardware"}, {id: 21, name: "Equipment and Business Services"}, {id: 22, name: "Food, Beverage, & Tobacco"}, {id: 23, name: "Magnets & Buttons, Stickers & Decals"}, {id: 24, name: "Electronics & Tech Products"}],
	//      "other": [{id: 25, name: "Automotive Accessories"}, {id: 26, name: "Awards & Trophies"}, {id: 27, name: "Publications"}, {id: 28, name: "Storage & Organization"}, {id: 29, name: "Other"}],
	//    }

	ctx.JSON(http.StatusOK, gin.H{
		"status": 200,
		"data":   cateOut,
		"msg":    "获取分类列表成功",
	})
	return
}

// 获取热销category
func CategoryHot(ctx *gin.Context) {
	var category tables.Category
	categorysHot, err := category.CategoryHot(ctx, global.DB)
	if err != nil {
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg": "查询失败",
		})
		return
	}

	categoryHotLi := make([]dto_out.CategoryHot, 0, 3)
	for _, cate := range *categorysHot {
		categoryOutInfoType := dto_out.CategoryHot{
			ID:       cate.ID,
			Name:     cate.Name,
			IsHot:    cate.IsHot,
			BgColor:  cate.BgColor,
			HotImage: string(cate.HotImage),
		}
		categoryHotLi = append(categoryHotLi, categoryOutInfoType)
	}

	ctx.JSON(http.StatusOK, gin.H{
		"status": 200,
		"data":   categoryHotLi,
		"msg":    "获取热销分类列表成功",
	})
	return
}

//func CategoryCreate(ctx *gin.Context) {
//	var categoryCreate forms.CategoryCreate
//	zap.S().Debugf("创建分类%v", categoryCreate)
//	err := ctx.ShouldBind(&categoryCreate)
//	if err != nil {
//		vali_tran.HandleValidatorError(ctx, err)
//		return
//	}
//
//	tc := tables.Category{
//		Name: categoryCreate.Name,
//	}
//	result := tc.CategoryExist(ctx)
//	if result.RowsAffected != 0 {
//		zap.S().Infof("%s 分类已存在", result.Error)
//		ctx.JSON(http.StatusBadRequest, gin.H{
//			"msg": "分类已存在",
//		})
//		return
//	}
//
//	tx := global.DB.Begin()
//	result = tx.WithContext(ctx).Create(&tc)
//	if result.Error != nil {
//		ctx.JSON(http.StatusBadRequest, gin.H{
//			"msg": "分类创建失败",
//		})
//		tx.Rollback()
//		return
//	}
//	tx.Commit()
//
//	ci := dto_out.CategoryInfo{
//		ID:       tc.ID,
//		Name:     tc.Name,
//		CreateAt: tc.CreatedAt.Format("2006-01-02 15:04:05"),
//	}
//
//	ctx.JSON(http.StatusOK, gin.H{
//		"status": 200,
//		"data":   ci,
//		"msg":    "创建分类成功",
//	})
//	return
//}

//func CategoryDelete(ctx *gin.Context) {
//	var categoryDelete forms.CategoryDelete
//	zap.S().Debugf("删除分类%v", categoryDelete)
//	err := ctx.ShouldBind(&categoryDelete)
//	if err != nil {
//		vali_tran.HandleValidatorError(ctx, err)
//		return
//	}
//
//	tx := global.DB.Begin()
//	tc := tables.Category{
//		BaseModel: tables.BaseModel{ID: categoryDelete.ID},
//	}
//	result := tx.WithContext(ctx).Delete(&tc)
//	if result.Error != nil {
//		ctx.JSON(http.StatusBadRequest, gin.H{
//			"msg": "分类删除失败",
//		})
//		tx.Rollback()
//		return
//	}
//	tx.Commit()
//
//	ctx.JSON(http.StatusOK, gin.H{
//		"status": 200,
//		"data":   categoryDelete.ID,
//		"msg":    "删除分类成功",
//	})
//	return
//}

//func CategoryUpdate(ctx *gin.Context) {
//	var categoryUpdate forms.CategoryUpdate
//	zap.S().Debugf("删除分类%v", categoryUpdate)
//	err := ctx.ShouldBind(&categoryUpdate)
//	if err != nil {
//		vali_tran.HandleValidatorError(ctx, err)
//		return
//	}
//
//	tc := tables.Category{
//		BaseModel: tables.BaseModel{ID: categoryUpdate.ID},
//	}
//	result := tc.CategoryExist(ctx)
//	if result.RowsAffected == 0 {
//		zap.S().Infof("%s 分类不存在", result.Error)
//		ctx.JSON(http.StatusBadRequest, gin.H{
//			"msg": "分类不存在",
//		})
//		return
//	}
//
//	tn := tables.Category{
//		Name: categoryUpdate.Name,
//	}
//	result = tn.CategoryExist(ctx)
//	if result.RowsAffected != 0 {
//		zap.S().Infof("%s 名字已存在", result.Error)
//		ctx.JSON(http.StatusBadRequest, gin.H{
//			"msg": "名字已存在",
//		})
//		return
//	}
//
//	err = tc.CategoryUpdate(ctx, categoryUpdate.Name)
//	if err != nil {
//		ctx.JSON(http.StatusBadRequest, gin.H{
//			"msg": "分类更新失败",
//		})
//		return
//	}
//
//	ci := dto_out.CategoryInfo{
//		ID:       tc.ID,
//		Name:     tc.Name,
//		CreateAt: tc.CreatedAt.Format("2006-01-02 15:04:05"),
//	}
//
//	ctx.JSON(http.StatusOK, gin.H{
//		"status": 200,
//		"data":   ci,
//		"msg":    "更新分类成功",
//	})
//	return
//}
